package org.canx.service;

import org.canx.domain.User;
import org.canx.repository.UserRepository;
import org.canx.utils.Response;
import org.canx.utils.ResultHttpStatus;
import org.canx.utils.Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * @class description :
 *
 *
 * @author Enough Wang
 * @time 2017年7月25日
 */
    @Service
    public class LoginService {

    private static Logger logger = Logger.getLogger(LoginService.class);

        @Autowired
        UserRepository userRepo;

        @Autowired
        TokenService tokenService;

        //验证员工Code登录
        public Response checkCode(String  userId, HttpServletResponse re){
            Response response = new Response();
            //验证携带此userId的员工在数据库中的距离是否为空
            try {
                User user = userRepo.findByUserGid(userId);
                if(user != null && user.getUserStatus().equals("1")){
                    response.setStatus(ResultHttpStatus.OK.getValue());
                    response.setMsg(user.getUserName()+":Login success");
                }
                //检查用户状态是否被禁用
                else if (user.getUserStatus().equals("0")){
                    response.setStatus(ResultHttpStatus. LOGIN_DENIED.getValue());
                    response.setMsg("Login denied ");
                }
                else {
                    response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                    response.setMsg("Login failed ");
                }
                logger.info("Login" + response.getStatus() + "," + response.getMsg());
            }
            catch (Exception ex){
                response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                response.setMsg("Login failed ");
                logger.info("Login failed" + ex);
            }
            return response;
        }

        //验证员工密码登录
        public Response checkPass(String userId, String pass,HttpServletResponse re){
            Response response = new Response();
            try {
                Util util = new Util();
                String passMD5 = util.getMD5(pass);
                User user = userRepo.findByUserGidAndUserPassword(userId,passMD5);
                if(user != null && user.getUserStatus().equals("1")){
                    response.setStatus(ResultHttpStatus.OK.getValue());
                    response.setMsg(user.getUserName()+":Login success");
                    response.setData(tokenService.createNewToken(user.getUserName()));
                } else if (user.getUserStatus().equals("0")){
                    response.setStatus(ResultHttpStatus. LOGIN_DENIED.getValue());
                    response.setMsg("Login denied ");
                } else {
                    response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                    response.setMsg("Login failed ");
                }
                logger.info("Login" + response.getStatus() + "," + response.getMsg());
            }
            catch (Exception ex){
                response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                response.setMsg("Login failed ");
                logger.info("Login failed" + ex);
            }
            return response;
        }
    }



